package base.application;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Resources;

public class MyDialogBuilder {

	private static MyDialogBuilder mDialogBuilder;

	public static MyDialogBuilder Build(Context context) {
		mDialogBuilder = new MyDialogBuilder(context);
		return mDialogBuilder;
	}

	private Context mContext;

	public MyDialogBuilder(Context context) {
		mContext = context;
	}

	/**
	 * Show the config dialog
	 * 
	 * @param msgRes
	 * @param titleRes
	 * @param configBtnRes
	 * @param confirmListener
	 * @param cancelBtnRes
	 * @param cancelListener
	 */
	public void showConfigDialog(int titleRes, int msgRes, int configBtnRes, final OnClickListener confirmListener, int cancelBtnRes, final OnClickListener cancelListener) {
		AlertDialog.Builder builder = new Builder(mContext);
		if (msgRes > 0) {
			builder.setMessage(getResources().getString(msgRes));
		}
		if (titleRes > 0) {
			builder.setTitle(getResources().getString(titleRes));
		}
		if (configBtnRes > 0) {
			builder.setPositiveButton(getResources().getString(configBtnRes), new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					if (confirmListener != null)
						confirmListener.onClick(dialog, which);
				}

			});
		}
		if (cancelBtnRes > 0) {
			builder.setNegativeButton(getResources().getString(cancelBtnRes), new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.cancel();
					if (cancelListener != null)
						cancelListener.onClick(dialog, which);
				}
			});
		}

		builder.create().show();
	}

	/**
	 * Show select dialog
	 * 
	 * @param titleRes
	 * @param items
	 * @param itemsListener
	 */
	public void showSelectDialog(int titleRes, int[] items, final OnClickListener itemsListener) {
		int length = items.length;
		if (length <= 0) {
			return;
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		if (titleRes > 0)
			builder.setTitle(titleRes);

		String[] itemsString = new String[length];
		for (int i = 0; i < length; i++) {
			itemsString[i] = getResources().getString(items[i]);
		}
		builder.setItems(itemsString, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				if (itemsListener != null)
					itemsListener.onClick(dialog, which);
			}
		}).show();
	}

	protected Resources getResources() {
		return mContext.getResources();
	}

	public void showWaitingDialog() {
		ProgressDialog dialog = new ProgressDialog(mContext);
		dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		dialog.show();
	}
	
}
